package tech.runchen.gz5055.oms.modules.openapi.controller;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.modules.openapi.form.ProductForm;
import tech.runchen.gz5055.oms.modules.system.entity.ProductEntity;
import tech.runchen.gz5055.oms.modules.system.service.IProductService;

import java.util.List;

/**
 * @author zzj
 */
@RestController
@RequestMapping("/openapi/product")
public class OpenApiProductController {

    @Autowired
    private IProductService productService;

    @PostMapping("/push")
    public Result push(@Validated @RequestBody List<ProductForm> productList) {
        for (ProductForm form : productList) {
            ProductEntity product = productService.queryInfoByProductId(form.getProductId());
            if (product == null) {
                product = new ProductEntity();
                BeanUtil.copyProperties(form, product);
                productService.saveProduct(product);
            } else {
                BeanUtil.copyProperties(form, product);
                productService.updateProduct(product);
            }
        }
        return Result.success("操作成功");
    }
}
